<%--
  Created by IntelliJ IDEA.
  User: ifut
  Date: 2020/7/29
  Time: 8:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <title> - 登录</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <link href="css/login.css" rel="stylesheet">
    <!--[if lt IE 9]>
    <meta http-equiv="refresh" content="0;ie.html" />
    <![endif]-->
    <script>
        // if (window.top !== window.self) {
        //     window.top.location = window.location;
        // }
    </script>
</head>
<body class="signin">
<div class="signinpanel">
    <div class="row">
        <div class="col-sm-12">
            <h4 class="no-margins">登录：</h4>
            <!-- <p class="m-t-md">登录到H+后台主题UI框架</p> -->
            <input id="username" type="text" class="form-control uname" placeholder="用户名" />
            <input id="password" type="password" class="form-control pword m-b" placeholder="密码" />
            <div class="verify clearfix">
                <div class="left fl">
                    <input id="verify" type="text" class="form-control" placeholder="输入验证码">
                </div>
                <div class="right fr" id="getCode">
                </div>
            </div>
            <!-- <a href="">忘记密码了？</a> -->
            <p><a href="./register.jsp">注册一个新账号</a></p>
            <button id="login_btn" class="btn btn-success btn-block m-b">登录</button>
        </div>
    </div>
    <div class="signup-footer">
        <div class="pull-left">
            &copy; hAdmin
        </div>
    </div>
</div>
<script src="js/jquery.min.js"></script>
<script>
    $(document).ready(() => {
        <%
        String msg = request.getParameter("msg");
        if(msg != null)
            out.print("alert('" + msg + "')");
        %>
    })


    window.onload = function() {
        //点击切换二维码
        changeCode();
        // 获取验证码
        getVailImg();
        // 初始化登录
        initLogin();

    }
    //点击切换二维码
    function changeCode() {
        $("#getCode").click(function () {
            $(this).find('img').remove();
            getVailImg();
        })
    }
    // 获取验证码
    function getVailImg() {
        var xmlhttp;
        xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", "${pageContext.request.contextPath}/getVailImg", true);
        xmlhttp.responseType = "blob";
        // 同理这里也需要
        xmlhttp.withCredentials = true;
        xmlhttp.onload = function () {
            if (this.status == 200) {
                var blob = this.response;
                var img = document.createElement("img");
                img.onload = function (e) {
                    window.URL.revokeObjectURL(img.src);
                };
                img.src = window.URL.createObjectURL(blob);
                document.querySelector("#getCode").appendChild(img);
            }
        }
        xmlhttp.send();
    }
    // 初始化登录
    function initLogin() {
        var usernameElem = document.querySelector('#username');
        var passwordElem = document.querySelector('#password');
        var verifyElem = document.querySelector('#verify');
        var loginBtn = document.querySelector('#login_btn');
        var formData;
        var username,password,verify
        loginBtn.onclick = function() {
            username = usernameElem.value
            password = passwordElem.value
            verify = verifyElem.value
            console.log(username,password,verify)
            formData = new FormData();
            formData.append('name',username);
            formData.append('pwd',password);
            formData.append('code',verify);
            $.ajax({
                type: "POST",
                url: "${pageContext.request.contextPath}/login",
                // data: "name="+ username +"&pwd=" + password,
                data:formData,
                dataType:'json',
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                processData: false,
                contentType: false,
                success: function(data){
                    if(data.code === 200) {
                        console.log("登录成功")
                        console.log(data)
                        window.frames.location.href="${pageContext.request.contextPath}/index_v1.jsp"
                    } else {
                        alert("登录失败");
                    }
                },
                error:function(err){
                    console.log(err)
                }
            });
        }
    }
</script>
</body>
</html>